package cn.edu.hbpu.service.impl;

import cn.edu.hbpu.common.R;
import cn.edu.hbpu.entity.Goods;
import cn.edu.hbpu.entity.ThirdCategory;
import cn.edu.hbpu.handler.CustomException;
import cn.edu.hbpu.mapper.GoodsMapper;
import cn.edu.hbpu.mapper.ThirdCategoryMapper;
import cn.edu.hbpu.service.GoodsService;
import cn.edu.hbpu.utils.DateTimeFormat;
import cn.hutool.http.HttpStatus;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author lcy
 * @date 2022/8/18 18:54
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private ThirdCategoryMapper thirdCategoryMapper;


    @Override
    public R<String> changeStatus(String gId, int status) {
        if (status != 1 && status != 0) {
            throw new CustomException("请传入正常的状态值");
        }
        if (Objects.isNull(gId)) {
            throw new CustomException("请传入商品id");
        }
        // 1. 查询出该id所对应的数据
        Goods goods = goodsMapper.selectById(gId);
        if (Objects.isNull(goods)) {
            throw new CustomException("没有该商品");
        }
        // 2. 设置要修改的状态
        goods.setStatus(status);
        // 3. 调用方法修改账号状态
        goodsMapper.updateById(goods);
        // 4. 返回结果
        return new R<>(HttpStatus.HTTP_OK, "状态修改成功");
    }

    @Override
    public R<String> deleteGoods(String gId) {
        if (Objects.isNull(gId)) {
            throw new CustomException("请传入商品id");
        }
        // 1. 删除商品
        goodsMapper.deleteById(gId);
        // 2. 返回结果
        return new R<>(HttpStatus.HTTP_OK, "删除商品成功");
    }

    @Override
    public R<Page<Goods>> pagingQuery(String searchField, String searchText, String startDateTime, String endDateTime, int page, int pageSize) {
        // 1. 非空验证
        if (Objects.isNull(searchField) || Objects.isNull(searchText)) {
            throw new CustomException("请传入搜索字段或文本");
        }
        LocalDateTime start = null;
        LocalDateTime end = null;
        // 2. 将日期进行转换
        if (!Objects.isNull(startDateTime) && !Objects.isNull(endDateTime)) {
            start = DateTimeFormat.parseTimeToUtc(startDateTime);
            end = DateTimeFormat.parseTimeToUtc(endDateTime);
        }
        // 3. 将searchField转为下划线
        searchField = StringUtils.camelToUnderline(searchField);

        // 4. 创建Page对象
        Page<Goods> pageInfo = new Page<>(page, pageSize);

        List<Goods> goodsList = new ArrayList<>();
        // 5. 判断searchField的字段类型
        if (searchField.isEmpty()) {
            // 5.1 说明都不是，正常分页查询
            goodsList = goodsMapper.pagingGoodsWithThirdCategoryName((page - 1) * pageSize, pageSize);
            // 5.1.1 查询总数据条数
            Integer totalCount = goodsMapper.selectCount(null);
            // 5.1.2 将总数据条数保存到pageInfo对象中
            pageInfo.setTotal(totalCount);
        } else if ("create_time".equals(searchField) || "update_time".equals(searchField)) {
            // 5.2 创建时间和更新时间字段
            goodsList = goodsMapper.pagingGoodsWithThirdCategoryNameByDateTime(searchField, start, end, (page - 1) * pageSize, pageSize);
            // 5.2.1 查询满足创建时间或更新时间条件的数据条数
            Integer totalCount = goodsMapper.selectTotalCountByDateTime(searchField, start, end);
            // 5.2.2 将总数据条数保存到pageInfo对象中
            pageInfo.setTotal(totalCount);
        } else if ("t_name".equals(searchField)) {
            // 5.3 根据sName二级分类名搜索其下的所有三级分类
            // 5.3.1 创建二级分类的询构造器
            LambdaQueryWrapper<ThirdCategory> thirdCategoryWrapper = new LambdaQueryWrapper<>();
            // 5.3.2 查询二级分类名等于searchText数据行
            thirdCategoryWrapper.eq(ThirdCategory::getTName, searchText);
            // 5.3.3 查询
            ThirdCategory thirdCategory = thirdCategoryMapper.selectOne(thirdCategoryWrapper);
            if (!Objects.isNull(thirdCategory)) {
                // 5.3.4 获取二级分类id
                String tId = thirdCategory.getTId();
                // 5.3.5 创建三级分类的查询构造器
                LambdaQueryWrapper<Goods> goodsWrapper = new LambdaQueryWrapper<>();
                // 5.3.6 查询三级分类中sId等于searchText二级分类对应的sId
                goodsWrapper.eq(Goods::getTId, tId);
                // 5.3.7 查询该二级分类下所有的三级分类
                goodsMapper.selectPage(pageInfo, goodsWrapper);
                goodsList = pageInfo.getRecords();
                // 5.3.8 将二级分类名set到secondCategoryList中
                for (Goods goods : goodsList) {
                    goods.setTName(searchText);
                }
                // 5.3.9 返回结果
                return new R<>(HttpStatus.HTTP_OK, pageInfo);
            }
        } else {
            // 5.3 其它字段
            if ("status".equals(searchField)) {
                searchText = "上架".equals(searchText) ? "1" : "0";
            }

            goodsList = goodsMapper.pagingGoodsWithThirdCategoryNameByOtherField(searchField, searchText, (page - 1) * pageSize, pageSize);
            // 5.3.1 查询满足其它字段条件的数据条数
            Integer totalCount = goodsMapper.selectTotalCountByOtherField(searchField, searchText);
            // 5.3.2 将总数据条数保存到pageInfo对象中
            pageInfo.setTotal(totalCount);
        }
        // 6. 将数据添加到pageInfo中
        pageInfo.setRecords(goodsList);
        // 7. 返回结果
        return new R<>(HttpStatus.HTTP_OK, pageInfo);
    }
}
